//NEEDS MORE CONVERTING WORK...UNTESTED
using System.Collections.Generic;
using System.IO;
using System;
using System.Threading;
using UnityEngine;


/**
 * This is the deck class. It holds all the cards as a base reference.
 * 
 * @author Sean Wheeler
 *
 *
 */
public class Deck : ScriptableObject
{
	private List<Card> cards; //The storage of all the cards you own
	private List<Card> drawcards; //The randomized deck you will draw from
	
	
	
	/**
	 * Creates an empty deck with an empty shuffled counterpart.
	 */
	public Deck()
	{
		cards = new List<Card>();
		drawcards = shuffle();
	}
	
	
	/**
	 * Creates a deck with its shuffled counterpart
	 * @param cards The card list to store
	 */
	public Deck(List<Card> cards)
	{
		this.cards = cards;
		drawcards = shuffle();
	}
	
	/**
	 * Create a deck from a database, reading it in.
	 * @param fis The file input stream
	 
	public Deck(FileInputStream fis)
	{
		cards = new ArrayList<Card>();
		
		//Do Stuff Here
		
		drawcards = shuffle();
	}
	*/
	
	
	/**
	 * Draw a card from you list of draw cards
	 * @return
	 */
	public Card drawCard()
	{
		if(drawcards.Count == 0)
			drawcards = shuffle();
		Card temp = drawcards[0];
		drawcards.RemoveAt(0);
		return temp;
	}
	
	
	/**
	 * Shuffle the base deck. If it is empty, the shuffled is also empty
	 * @return a shuffled version of your deck
	 * @throws InterruptedException 
	 */
	public List<Card> shuffle()
	{
		List<Card> shuffled = new List<Card>();
		List<Card> tempList = new List<Card>();
		for(int i=0; i< cards.Count; i++) tempList.Add(cards[i].duplicate());
		Card temp;
		int j = cards.Count;
		System.Random r = new System.Random();
		while (j > 0)
		{
			int index = r.Next(j);
			temp = tempList[index];
			shuffled.Add(new Card(temp.getName(),temp.getActiveDescription(),temp.getPassiveDescription(),temp.getCost(), temp.getPriority(), temp.getType(), temp.getActives(), temp.getPassives()));
			tempList.RemoveAt(index);
			j--;
		}
		try{Thread.Sleep(11);}catch(Exception){} //This is done to insure different times are used when shuffling the decks
		if (cards.Count == 0) Debug.Log("Problem");
		return shuffled;
		
	}
	
	public void reshuffle(){
		this.drawcards = shuffle();
	}
	
	public string tostring()
	{
		string s = "Deck: ";
		foreach(Card c in cards)
		{
			s += c.tostring();
		}
		
		s+= "\nShuffledDeck: ";
		
		foreach(Card c in drawcards)
		{
			s += c.tostring();
		}
		return s;
	}
	
	public List<Card> getCards(){
		return cards;
	}
	
	public List<Card> getDrawCards(){
		return drawcards;
	}
}